import express from 'express';
import dbhelper from '../../lib/dbhelper';
let adminRouter = express.Router();
module.exports = adminRouter;
//获取首页信息的路由接口
adminRouter.get("/index", function (rqq, res, next) {
    let queryMenuSql = "SELECT * FROM menulist WHERE m_status2='on'";
    let webSql = "SELECT * FROM webinfo;";
    dbhelper.query(queryMenuSql, [], function (err, result) {
        if (!err) {
            dbhelper.query(webSql, [], function (err, result2) {
                if (!err) {
                    res.render("./admin/index.html", {
                        menu: result,
                        webInfo: result2
                    });
                }
            })
        }
    })
})
//渲染menu内容
adminRouter.get("/menu", function (req, res, next) {
    res.render("./admin/menu.html",{order:34});
})
//menu接口,查询menu数据
adminRouter.post("/getmenu", function (req, res, next) {
    let queryMenuSql = "SELECT * FROM menulist WHERE m_status=1 ORDER BY m_order DESC;";
    dbhelper.query(queryMenuSql, [], function (err, result) {
        if (!err) {
            let obj = {
                "status": 200,
                "message": "",
                "total": result.length,//显示记录总条数
                "rows": result
            }
            res.json(obj);
        }
    })
})
//menu接口,软删除(即将数据状态改为0,状态为1的才会显示到页面去)
adminRouter.post("/deleteMenu", function (req, res, next) {
    let updateMenuSql = "UPDATE menulist SET m_status=0 WHERE m_id=?;";
    let params = req.body.m_id;
    dbhelper.query(updateMenuSql, params, function (err, result) {
        if (!err) {
            if (result.affectedRows >= 1) {
                res.json({
                    msg: "删除成功",
                    status: 1
                });
            } else {
                res.json({
                    msg: "删除失败",
                    status: -1
                });
            }
        }
    })
})
//menu接口,修改menu数据
adminRouter.post("/updateMenu", function (req, res, next) {
    let queryMenuSql = "UPDATE menulist SET m_name=?,m_link=?,m_status=?,m_num=?,m_father=?,m_icon=? WHERE m_id=?;";
    let data = req.body;
    //时刻记得参数列表是一个数组,在这里吃了大亏
    let params = [data.m_name, data.m_link, data.m_status, data.m_num, data.m_father, data.m_icon, data.m_id]
    console.log(params);
    dbhelper.query(queryMenuSql, params, function (err, result) {
        if (!err) {
            console.log("111");
            if (result.affectedRows >= 1) {
                res.json({
                    msg: "修改成功",
                    status: 1
                });
            } else {
                res.json({
                    msg: "修改失败",
                    status: -1
                });
            }
        }
    })
})
//menu接口,修改菜单显示状态
adminRouter.post("/updateMenuStatus", function (req, res, next) {
    let queryMenuSql = "UPDATE menulist SET m_status2=? WHERE m_id=?;";
    let data = req.body;
    //时刻记得参数列表是一个数组,在这里吃了大亏
    let params = [data.m_status2,data.m_id];
    console.log(params);
    dbhelper.query(queryMenuSql, params, function (err, result) {
        if (!err) {
            console.log(result);
            if (result.affectedRows >= 1) {
                res.json({
                    msg: "修改菜单状态成功",
                    status: 1
                });
            } else {
                res.json({
                    msg: "修改菜单状态失败",
                    status: -1
                });
            }
        }
    })
})
//渲染菜单页面
adminRouter.get("/addMenu", function (req, res, next) {
    res.render("./admin/addMenu.html");
})
//addMenu接口,添加新数据
adminRouter.post("/addMenuData", function (req, res, next) {
    let insertMenuSql = "INSERT INTO menulist(`m_name`,`m_link`,`m_num`,`m_father`,`m_icon`,`m_order`,`m_status2`)VALUES(?,?,?,?,?,?,?);";
    let data = req.body;
    console.log(data.menu_status);
    
    let params = [
        data.menu_name,
        data.menu_link,
        data.menu_num,
        data.menu_father,
        data.menu_icon,
        data.menu_order,
        data.menu_status
    ]
    dbhelper.query(insertMenuSql, params, function (err, result) {
        console.log(result);
        if (!err) {
            if (result.affectedRows >= 1) {
                res.json({
                    msg: "菜单添加成功",
                    status: 1
                });
            } else {
                res.json({
                    msg: "菜单添加失败",
                    status: -1
                });
            }
        }
    })

})